package com.njtoyo.taxi.admin.rest.controller.open;

import cn.hutool.extra.servlet.ServletUtil;
import com.njtoyo.taxi.admin.aop.LoggerManage;
import com.njtoyo.taxi.admin.aop.PassToken;
import com.njtoyo.taxi.admin.mapper.master.enums.OnCallBackLogSourcePlatform;
import com.njtoyo.taxi.admin.rest.presenter.open.on_call.OrderReplyPresenter;
import com.njtoyo.taxi.admin.rest.wrapper.open.on_call.CallbackWrapper;
import com.njtoyo.taxi.admin.rest.wrapper.open.on_call.CreateWrapper;
import com.njtoyo.taxi.admin.service.open.OnCallService;
import com.taxi.entity.common.RestResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

@RestController
@RequestMapping("/open/on-call")
@Api(tags = "电召新版")
@Slf4j
public class OnCallController {
    
    @Autowired
    private OnCallService onCallService;
    
    @PostMapping("v1")
    @ApiOperation(value = "发单接口", notes = "发单接口，应该要做白名单限制")
    @LoggerManage(description = "添加电召记录V1")
    @PassToken
    public RestResult<OrderReplyPresenter> create(@Valid @RequestBody CreateWrapper wrapper,
                                                  @RequestHeader(name = "app") OnCallBackLogSourcePlatform app,
                                                  HttpServletRequest request) {
        
        String ip = ServletUtil.getClientIP(request);
        log.info("from app {}", app);
        log.info("client ip {}",  ip);
        return onCallService.create(wrapper, app);
    }
    
    @PostMapping("callback")
    @ApiOperation(value = "状态变更通知", notes = "订单状态变更通知")
    @LoggerManage(description = "订单状态变更通知")
    @PassToken
    public RestResult callback(@RequestBody CallbackWrapper wrapper) {
        return onCallService.callback(wrapper);
    }
}
